/**
 * @param {number} numCourses
 * @param {number[][]} prerequisites
 * @return {boolean}
 */
var canFinish = function(numCourses, prerequisites) {
  if(numCourses == 1) {
    return true;
  }
  function clearIndex(index) {
    for(let i = 0; i < numCourses; i++) {
      if(i != index && arr[i] && arr[i].indexOf(index) !== -1) {
        const tempIndex = arr[i].indexOf(index);
        arr[i].splice(tempIndex, 1);
      }
    }
  }
  let arr = [];
  for(let i = 0; i < numCourses; i++) {
    arr.push([]);
  }
  for(let i = 0; i < prerequisites.length; i++) {
    arr[prerequisites[i][0]].push(prerequisites[i][1]);
  }
  for(let i = 0; i < numCourses; i++) {
    for(let j = 0; j < numCourses; j++) {
      if(arr[j] && arr[j].length === 0) {
        clearIndex(j);
        arr[j] = undefined;
        break;
      }
      if(j == numCourses - 1) {
        return false;
      }
    }
  }
  return true;

};
let numCourses = 2, prerequisites = [[1,0]];
console.log(canFinish(numCourses, prerequisites));
numCourses = 2, prerequisites = [[1,0],[0,1]];
console.log(canFinish(numCourses, prerequisites));
